package com.hugh.commom.netty.client.handle.diapatcher;

import com.alibaba.fastjson.JSON;
import com.hugh.commom.RpcResponse;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ResponseDispatcherHandler extends SimpleChannelInboundHandler<RpcResponse> {

    private RequestPendingCenter requestPendingCenter;

    public ResponseDispatcherHandler(RequestPendingCenter requestPendingCenter) {
        this.requestPendingCenter = requestPendingCenter;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, RpcResponse responseMessage) throws Exception {
        log.info("client requestId :{}, MessageBody :{}",
                JSON.toJSON(responseMessage.getRequestId()),
                JSON.toJSON(responseMessage.getResponse()));
        requestPendingCenter.set(responseMessage.getRequestId(), responseMessage);
    }
}
